package com.ks.cms.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.cms.dao.BCmsClassMapper;
import com.ks.cms.entity.BCmsClass;
import com.ks.cms.entity.BCmsNews;
import com.ks.cms.service.IBCmsClassService;
import com.ks.cms.service.IBCmsNewsService;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.dto.OperateBase;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.exception.BizExceptionEnum;
import com.ks.core.exception.GunsException;
import com.ks.core.util.ToolUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 信息发布系统的信息栏目（按租户） 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-22
 */
@Service
public class BCmsClassServiceImpl extends ServiceImpl<BCmsClassMapper, BCmsClass> implements IBCmsClassService {
    @Resource
    private BCmsClassMapper bCmsClassMapper;
    @Resource
    private IBCmsNewsService ibCmsNewsService;

    @Override
    public Map<String, Object> selectCmsList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<BCmsClass> wrapper = new EntityWrapper<>();
        BCmsClass bCmsClass = new BCmsClass();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("id".equals(field.getName())) {
                    bCmsClass.setId(Integer.getInteger(field.getValue()));
                }
            }
        }
        bCmsClass.setTenantID(context.getTenantId().intValue());
        bCmsClass.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(bCmsClass);
        Integer integer = bCmsClassMapper.selectCount(wrapper);
        map.put("count", integer);
        List<BCmsClass> bCmsClasses = bCmsClassMapper.selectPage(new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit()), wrapper);
        if (ToolUtil.isNotEmpty(bCmsClasses)) {
            for (BCmsClass cmsClass : bCmsClasses) {
                if (ToolUtil.isNotEmpty(cmsClass.getIFartherClassID())) {
                    BCmsClass aClass = bCmsClassMapper.selectById(cmsClass.getIFartherClassID());
                    if (ToolUtil.isNotEmpty(aClass)) {
                        cmsClass.setSuperName(aClass.getIClassName());
                    }
                }
            }
        }
        map.put("list", bCmsClasses);
        return map;
    }

    @Override
    public int addCms(OperationContext context, BCmsClass bCmsClass) {
        bCmsClass.setTenantID(context.getTenantId().intValue());
        bCmsClass.setCreateUserID(context.getUserId().intValue());
        bCmsClass.setOrgID(context.getOrgId().intValue());
        bCmsClass.setDataType("TENANT");
        Integer i = bCmsClassMapper.insert(bCmsClass);
        return i;
    }

    @Override
    public int updateCms(OperationContext context, BCmsClass bCmsClass) {
        if (bCmsClass.getId().equals(bCmsClass.getIFartherClassID())) {
            throw new GunsException(BizExceptionEnum.CMS_CLASS_ERROR);
        }
        EntityWrapper<BCmsClass> wrapper = new EntityWrapper<>();
        BCmsClass cmsClass = new BCmsClass();
        cmsClass.setIFartherClassID(String.valueOf(bCmsClass.getId()));
        cmsClass.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(cmsClass);
        List<BCmsClass> bCmsClasses = bCmsClassMapper.selectList(wrapper);
        if (ToolUtil.isNotEmpty(bCmsClasses)) {
            for (BCmsClass cmsClass1 : bCmsClasses) {
                if (bCmsClass.getIFartherClassID().equals(cmsClass1.getId())){
                    throw new GunsException(BizExceptionEnum.CMS_CLASS_ADD_ERROR);
                }

            }
        }
        bCmsClass.setUpdateUserID(context.getUserId().intValue());
        bCmsClass.setUpdateTime(new Date());
        Integer i = bCmsClassMapper.updateById(bCmsClass);
        return i;
    }

    @Override
    public int deleteCms(OperationContext context, int id) {
        //这里需要判断是单条操作还是多条操作
        EntityWrapper<BCmsClass> wrapper = new EntityWrapper<>();
        BCmsClass cmsClass = new BCmsClass();
        cmsClass.setIFartherClassID(String.valueOf(id));
        cmsClass.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(cmsClass);
        List<BCmsClass> bCmsClasses = bCmsClassMapper.selectList(wrapper);
        if (ToolUtil.isNotEmpty(bCmsClasses)) {
            throw new GunsException(BizExceptionEnum.CMS_CLASS_DELETE_ERROR);
        }
        EntityWrapper<BCmsNews> entityWrapper = new EntityWrapper<>();
        BCmsNews cmsNews = new BCmsNews();
        cmsNews.setCClassID(id);
        cmsNews.setDelMark(Const.DEL_FLAG_NORMAL);
        entityWrapper.setEntity(cmsNews);
        List<BCmsNews> bCmsNews = ibCmsNewsService.selectList(entityWrapper);
        if (ToolUtil.isNotEmpty(bCmsNews)) {
            throw new GunsException(BizExceptionEnum.CMS_NEW_DELETE_ERROR);
        }
        BCmsClass cmsClass1 = bCmsClassMapper.selectById(id);
        cmsClass1.setDelMark(Const.DEL_FLAG_DEL);
        Integer integer = bCmsClassMapper.updateById(cmsClass1);
        return integer;
    }

    public Map<String, Object> isResultMap(List list, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", list.size());
        map.put("limit", searchCriteriaBase.getLimit());
        map.put("pageIndex", searchCriteriaBase.getPageIndex());
        map.put("hasMore", false);
        map.put("items", list);
        return map;
    }

    /*******************************************************************************************************/
   /*
   信息发布服务管理模块-添加接口
   @Override
    public int addBCmsClass(OperationContext operationContext,BCmsClass bCmsClass) {
        Integer insert = bCmsClassMapper.insert(bCmsClass);
        return insert;
    }


     //信息发布服务管理模块-删除接口
    @Override
    public int deleteBCmsClass(OperationContext operationContext, OperateBase operateBase) {
        BCmsClass bCmsClass = new BCmsClass();
        if (!operateBase.isMultiple()) {
            bCmsClass.setId(Integer.valueOf(operateBase.getDataId()));
            BCmsClass cmsClass = bCmsClassMapper.selectById(bCmsClass);
            if (ToolUtil.isEmpty(cmsClass)) {
                throw new GunsException("该栏目不存在");
            }
            bCmsClass.setDelMark(Const.DEL_FLAG_DEL);
            return bCmsClassMapper.updateById(bCmsClass);
        }else {
            for (String str : operateBase.getIdList()) {
                bCmsClass.setId(Integer.valueOf(str));
                BCmsClass cmsClass = bCmsClassMapper.selectById(bCmsClass);
                if (ToolUtil.isEmpty(cmsClass)) {
                    throw new GunsException("该栏目不存在");
                }
                bCmsClass.setDelMark(Const.DEL_FLAG_DEL);
                return bCmsClassMapper.updateById(bCmsClass);
            }
        }
        return 0;
    }


     //  信息发布服务管理模块-栏目列表查询接口



    @Override
    public Map<String, Object> selectBCmsClassList(OperationContext operationContext,SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<BCmsClass> wrapper = new EntityWrapper<>();
        BCmsClass bCmsClass = new BCmsClass();
        for (SearchCriteriaBase.matchField field : fields) {
            if ("id".equals(field.getName())) {
                bCmsClass.setId(Integer.valueOf(field.getValue()));
            }
             bCmsClass.setDelMark(Const.DEL_FLAG_NORMAL);
             bCmsClass.setOrgID(operationContext.getOrgId().intValue());
             bCmsClass.setOrgSubID(operationContext.getOrgSubId().intValue());
             bCmsClass.setTenantID(operationContext.getTenantId().intValue());
            wrapper.setEntity(bCmsClass);
            if(searchCriteriaBase.getStartTime()!=null&&searchCriteriaBase.getEndTime()!=null){
            String filterSql="createTime> '"+searchCriteriaBase.getStartTime()
                    +"' and createTime<'"+searchCriteriaBase.getEndTime()+"'";
            wrapper.addFilter(filterSql);
        }
            List<BCmsClass> bCmsClasses = bCmsClassMapper.selectList(wrapper);
            if(ToolUtil.isNotEmpty(bCmsClasses)){
                Map<String, Object> resultMap = this.isResultMap(bCmsClasses, searchCriteriaBase);
                return resultMap;
            }
        }
        return null;
    }
    private Map<String, Object> isResultMap(List<BCmsClass> bCmsClasses, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", bCmsClasses.size());
        map.put("limit", searchCriteriaBase.getLimit());
        map.put("pageIndex", searchCriteriaBase.getPageIndex());
        map.put("hasMore", false);
        map.put("items", bCmsClasses);
        return map;
    }*/
}
